export default {

	/**
	 * 生成海报获取文字
	 * @param string text 为传入的文本
	 * @param int num 为单行显示的字节长度
	 * @return array 
	 */
	textByteLength: function(text, num) {
		let strLength = 0;
		let rows = 1;
		let str = 0;
		let arr = [];
		for (let j = 0; j < text.length; j++) {
			if (text.charCodeAt(j) > 255) {
				strLength += 2;
				if (strLength > rows * num) {
					strLength++;
					arr.push(text.slice(str, j));
					str = j;
					rows++;
				}
			} else {
				strLength++;
				if (strLength > rows * num) {
					arr.push(text.slice(str, j));
					str = j;
					rows++;
				}
			}
		}
		arr.push(text.slice(str, text.length));
		return [strLength, arr, rows] //  [处理文字的总字节长度，每行显示内容的数组，行数]
	},
	/**
	 * 绘制圆角矩形
	 * @param {*} x 起始点x坐标
	 * @param {*} y 起始点y坐标
	 * @param {*} w 矩形宽
	 * @param {*} h 矩形高
	 * @param {*} r 圆角半径
	 * @param {*} ctx 画板上下文
	 * @param {*} arr 圆角方向 [1,1,1,1]
	 */
	darwRoundRect(x, y, w, h, r, ctx, arr = [1, 1, 1, 1]) {
		ctx.save()
		ctx.beginPath()
		if (arr[0] == 1) {
			// 左上弧线
			ctx.arc(x + r, y + r, r, 1 * Math.PI, 1.5 * Math.PI)
			// 左直线
			ctx.moveTo(x, y + r)
		} else {
			ctx.moveTo(x, y)
		}
		if (arr[1] == 1) {
			ctx.lineTo(x, y + h - r)
			// 左下弧线
			ctx.arc(x + r, y + h - r, r, 0.5 * Math.PI, 1 * Math.PI)
			// 下直线
			ctx.lineTo(x + r, y + h)
		} else {
			ctx.lineTo(x, y + h)
		}
		if (arr[2] == 1) {
			ctx.lineTo(x + w - r, y + h)
			// 右下弧线
			ctx.arc(x + w - r, y + h - r, r, 0 * Math.PI, 0.5 * Math.PI)
			// 右直线
			ctx.lineTo(x + w, y + h - r)
		} else {
			ctx.lineTo(x + w, y + h)
		}
		if (arr[3] == 1) {
			ctx.lineTo(x + w, y + r)
			// 右上弧线
			ctx.arc(x + w - r, y + r, r, 1.5 * Math.PI, 2 * Math.PI)
			// 上直线
			ctx.lineTo(x + w - r, y)
			ctx.lineTo(x + r, y)
		} else {
			ctx.lineTo(x + w, y)
		}
		if (arr[0] == 1) {
			ctx.lineTo(x + r, y)
		} else {
			ctx.moveTo(x, y)
		}
	},
	/**
	 * 获取分享海报
	 * @param array arr2 海报素材 [海报主图，分享码]
	 * @param string store_name 素材文字
	 * @param string price 价格
	 * @param function successFn 回调函数
	 * 
	 * 
	 */
	PosterCanvas: function(arr2, store_name, price, devicePixelRatio) {
		try {
			console.log('arr2, store_name, price, devicePixelRatio',arr2, store_name, price, devicePixelRatio)
			let that = this;
			return new Promise((resolve, reject) => {
				uni.showLoading({
					title: '海报生成中',
					mask: true
				});
				const rpx = (num) => uni.upx2px(num);
				// 自定义组件需要通过this.$scoped访问实例
				const ctx = uni.createCanvasContext('firstCanvas', this.$scoped);
				ctx.clearRect(0, 0, 0, 0);
				// console.log(arr2)
				/**
				 * 只能获取合法域名下的图片信息,本地调试无法获取
				 * 
				 */
				ctx.fillStyle = '#fff';
				const CANVASWIDTH = rpx(600);
				const CANVASHEIGHT = rpx(718);
				that.darwRoundRect(0, 0, CANVASWIDTH, CANVASHEIGHT, rpx(24), ctx)
				ctx.setFillStyle('white')
				ctx.fill()
				// ctx.fillRect(0, 0, rpx(480), rpx(694));
				uni.getImageInfo({
					src: arr2[0],
					success: function(res) {
						console.log(res)
						uni.getImageInfo({
							src: arr2[1],
							success: function(res1) {
								const WIDTH = rpx(600);
								const HEIGHT = rpx(380);
								let contentHh = rpx(34);
								console.log(WIDTH, HEIGHT, contentHh)
								// ctx.drawImage(arr2[0], 0, 0, WIDTH, 1050); 
								that.darwRoundRect(0, 0, WIDTH, HEIGHT, rpx(24),
									ctx, [1, 0, 0,
										1
									])
								ctx.clip();
								ctx.drawImage(res.path, 0, 0, WIDTH, HEIGHT);
								let w = rpx(152);
								let cx = rpx(416);
								let cy = rpx(542);
								ctx.restore();
								ctx.drawImage(res1.path, cx, cy, w, w);
								ctx.restore();
								ctx.setTextAlign('left')
								ctx.setFontSize(rpx(28));
								ctx.setFillStyle('#FE5A1C');
								ctx.fillText('¥ ', rpx(24), rpx(602) + contentHh);
								ctx.setFontSize(rpx(48));
								ctx.fillText(price, rpx(48), rpx(602) + contentHh);
								const CONTENT_ROW_LENGTH = 40;
								let [contentLeng, contentArray, contentRows] = that
									.textByteLength(store_name,
										CONTENT_ROW_LENGTH);
								if (contentRows > 4) {
									contentRows = 4;
									let textArray = contentArray.slice(0, 2);
									textArray[textArray.length - 1] += '…';
									contentArray = textArray;
								}
								ctx.setTextAlign('left');
								ctx.setFontSize(rpx(28));
								ctx.setFillStyle('#333');
								// let contentHh = 36 * 1.5;
								for (let m = 0; m < contentArray.length; m++) {
									// ctx.fillText(contentArray[m], 50, 1000 + contentHh * m,750);
									if (m) {
										ctx.fillText(contentArray[m], rpx(24),
											HEIGHT + rpx(
												50) + contentHh * m,
											rpx(552));
									} else {
										ctx.fillText(contentArray[m], rpx(24),
											HEIGHT + rpx(
												50) + contentHh * m,
											rpx(552));
									}
								}
								// ctx.setTextAlign('left')
								// ctx.setFontSize(rpx(20));
								// ctx.setFillStyle('#999');
								// ctx.fillText('长按或扫描查看', rpx(314), rpx(628) + contentHh);
								// console.log(arr2[0])
								// console.log(arr2[1])
								// console.log(arr2[2])
								console.log(devicePixelRatio, 111)
								ctx.draw(true, setTimeout(function() {
									uni.canvasToTempFilePath({
										canvasId: 'firstCanvas',
										fileType: 'png',
										width: CANVASWIDTH,
										height: CANVASHEIGHT,
										destWidth: CANVASWIDTH *
											devicePixelRatio,
										destHeight: CANVASHEIGHT *
											devicePixelRatio,
										success: function(res) {
											uni
												.hideLoading();
											resolve(res
												.tempFilePath
											);
										},
										fail: function(err) {
											uni
												.hideLoading();
											reject(err)
											console.log(err)
										}
									}, that)
								}, 2000));
							}
						})
					},
					fail: function(err) {
						console.log('失败', err)
						reject(err)
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							title: '无法获取图片信息'
						});
					}
				})
			})
		} catch (err) {
			console.log(err)
		}
	},
	/**
	 * 获取分享海报
	 * @param array arr2 海报素材 [海报主图，分享码]
	 * @param string user_name 邀请人姓名
	 * @param string price invite_id 邀请人Id
	 * 
	 * 
	 */
	InviteCanvas: function(arr2, canvasId, user_name, invite_id, devicePixelRatio) {
		try {
			let that = this;
			return new Promise((resolve, reject) => {
				const rpx = (num) => uni.upx2px(num);
				// 自定义组件需要通过this.$scoped访问实例
				const ctx = uni.createCanvasContext(canvasId, that.$scoped);
				ctx.clearRect(0, 0, 0, 0);
				// console.log(arr2)
				/**
				 * 只能获取合法域名下的图片信息,本地调试无法获取
				 * 
				 */
				ctx.fillStyle = '#fff';
				const CANVASWIDTH = rpx(572);
				const CANVASHEIGHT = rpx(994);
				that.darwRoundRect(0, 0, CANVASWIDTH, CANVASHEIGHT, rpx(16), ctx)
				ctx.setFillStyle('white')
				ctx.fill()
				// ctx.fillRect(0, 0, rpx(480), rpx(694));
						console.log('arr----',arr2, canvasId, user_name, invite_id, devicePixelRatio)
				uni.getImageInfo({
					src: arr2[0],
					success: function(res) {
						console.log('成功',res)
						uni.getImageInfo({
							src: arr2[1],
							success: function(res1) {
								const WIDTH = rpx(572);
								const HEIGHT = rpx(800);
								// ctx.drawImage(arr2[0], 0, 0, WIDTH, 1050); 
								that.darwRoundRect(0, 0, WIDTH, HEIGHT, rpx(24),
									ctx, [1, 0, 0,
										1
									])
								ctx.clip();
								ctx.drawImage(res.path, 0, 0, WIDTH, HEIGHT);
								ctx.restore();
								that.darwRoundRect(rpx(26), rpx(772), rpx(128), rpx(
										128), rpx(64),
									ctx)
								ctx.setFillStyle('white')
								ctx.fill()
								let w = rpx(100);
								let cx = rpx(40);
								let cy = rpx(842);
								ctx.restore();
								that.darwRoundRect(cx, cy, w, w, rpx(50),
									ctx)
								ctx.clip();
								ctx.drawImage(res1.path, cx, cy, w, w);
								// ctx.drawImage(res1.path, cx, cy, w, w);
								ctx.restore();
								ctx.setTextAlign('left')
								ctx.setFontSize(rpx(32));
								ctx.setFillStyle('#333333');
								ctx.fillText(user_name, rpx(172), rpx(878));
								ctx.restore();
								ctx.setFontSize(rpx(28));
								ctx.setFillStyle('#333333');
								ctx.fillText('邀请码：' + invite_id, rpx(172), rpx(
									922));
								ctx.draw(true, setTimeout(function() {
									uni.canvasToTempFilePath({
										canvasId: canvasId,
										fileType: 'png',
										width: CANVASWIDTH,
										height: CANVASHEIGHT,
										destWidth: CANVASWIDTH *
											devicePixelRatio,
										destHeight: CANVASHEIGHT *
											devicePixelRatio,
										success: function(res) {
											resolve(res
												.tempFilePath
											);
										},
										fail: function(err) {
											reject(err)
											console.log(err)
										}
									}, that)
								}, 2000));
							}
						})
					},
					fail: function(err) {
						console.log('失败', err)
						reject(err)
						uni.showToast({
							icon: 'none',
							title: '无法获取图片信息'
						});
					}
				})
			})
		} catch (err) {
			console.log(err)
		}
	},
}